//index @zw

//模拟数据(约定格式)
var jsonMenu = [{"name":"商家管理","child":[{"name":"区域分类","url":"areaType.html"},{"name":"商家数据","url":"merData.html"},{"name":"数据采集","url":"dataCell.html"}]},
				{"name":"商品管理","child":[{"name":"商品品牌","url":"brand.html"},{"name":"商品分类","url":"goodsType.html"},{"name":"商品数据","url":"33"}]},
				{"name":"会员管理","url":"5"},
				{"name":"体验管理","url":""},{"name":"金融服务","url":""},{"name":"微信平台","url":""},
				{"name":"APP管理","url":""},{"name":"数据统计","url":""},{"name":"系统管理","url":""},];
//子菜单集合,url地址数组
var urList = {},urPath = [];

$(function(){
	//主菜单
	$menu = $(".nav-menu");
	//子菜单
	$childMenu = $("#panel-left");
	//主面板
	$panelRight = $("#panel-right");
	
	//control
	$menu.html(drawMenu(jsonMenu));
	//动态布局
	$childMenu.height(dyHeight(120,true));
	$panelRight.width(dyHeight(230,false));
	//主菜单点击
	$menu.on("click","#firstMenu li",function(){
		$menu.find("ul li").removeClass("menuActive");
		$(this).addClass("menuActive");
		var tempGetChild =  urList[$(this).index()]
		if(tempGetChild){
			$childMenu.html(drawChildMenu(tempGetChild));
			//模拟点击初始化
			$("#childMenu li").eq(0).addClass("childActive");
			$panelRight.load(urPath[0])
		}
	});
	//子菜单点击
	$childMenu.on("click","#childMenu li",function(){
		$childMenu.find("ul li").removeClass("childActive");
		$(this).addClass("childActive");
		$panelRight.load(urPath[$(this).index()],function(){
			
		})
			
	})
	//模拟点击初始化-临时解决方案
	$("#firstMenu li").eq(0).click();
	//子页面切换
	function changePage(){
		$addBtn = $("#addBtn"),$backBtn = $("#backBtn,#backBtn_q"),$queryLink=$("#query"),$show = $("#show"),$add = $("#add"),$query = $("#queryMer");
		$addBtn.on("click",function(){
			showHide($add,[$show,$query]);
			
		});
		$queryLink.on("click",function(){
			showHide($query,[$add,$show]);
		})
		$backBtn.on("click",function(){
			showHide($show,[$add,$query]);
		})
	}
	
	
})
//隐藏
function showHide(dom1,dom2){
	dom1.show();
	$.each(dom2,function(k,d){
		$(d).hide();	
	})
}
function dyHeight(tempNum,isHei){
	//屏幕宽高
	var winHeight = $(window).height(),winWidth = $(window).width();
	var temp = (isHei==true)?winHeight-tempNum:winWidth-tempNum;
	return temp;
}

//真实数据获取接口
function getData(){	

}
//拼接主菜单及保存子菜单
function drawMenu(menuData){
	var str="<ul id='firstMenu'>";
	for(var i =0;i<menuData.length;i++){
		//判断是否子菜单，然后缓存进全局变量
		if(menuData[i].child){
			//临时变量
			var childList = menuData[i].child,tempUrlList = [],tempNameList = [];
			for(var j = 0;j<childList.length;j++){
				tempUrlList.push(childList[j].url);
				tempNameList.push(childList[j].name)
			}
			urList[i] = {"url":tempUrlList,"name":tempNameList};
		}
		str+="<li>"+menuData[i].name+"</li>";
	}
	str+="</ul>";
	return str;
}
//拼接子菜单
function drawChildMenu(menuData){
	urPath = [];
	var str = "<ul id='childMenu'>"
	for(var i = 0;i<menuData["name"].length;i++){
		str+="<li>"+menuData["name"][i]+"</li>";
		//缓存url地址
		urPath.push(menuData["url"][i])
	}
	str+="</ul>";
	return str;
}